package com.mycompany.project.client;

import com.bramosystems.oss.player.core.client.AbstractMediaPlayer;
import com.bramosystems.oss.player.core.client.LoadException;
import com.bramosystems.oss.player.core.client.PluginNotFoundException;
import com.bramosystems.oss.player.core.client.ui.NativePlayer;
import com.gwtext.client.widgets.Window;
import com.bramosystems.oss.player.core.event.client.LoadingProgressHandler;
import com.bramosystems.oss.player.core.event.client.LoadingProgressEvent;
import com.gwtext.client.widgets.layout.RowLayout;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.gwtext.client.widgets.event.PanelListenerAdapter;
import com.gwtext.client.widgets.Panel;
import com.bramosystems.oss.player.core.event.client.PlayStateHandler;
import com.bramosystems.oss.player.core.event.client.PlayStateEvent;


/**
 * @author Laboratorio Tv Collaborativa/Contenuti Digitali --> Sardegna District --> Crs4 --> Sardegna Ricerche
 * 
 * This class extends {@link Window} and implements a @SaStria media player
 * full screen Window. It differs from {@link Window} for the management
 * of close FullScreenEvent Handler. When the full screen window is closed  
 * it fire the FullScreenEventHandler event to @SaStria listeners.
 *
 */
public class PlayerFullScreen extends Window {
    private AbstractMediaPlayer medPlayer;   // full screen media player
    private HandlerManager handleManager=new HandlerManager(null); // handle Manager for FullScreenEventHandler
    private boolean videoPlaying;  // if true video is in play status, false otherwise.
       
    
    /**
     * Constructor of this class. it sets media player, size and the FullScreenEventHandler fire manager.  
     * @param video :  String value with the URL of video to open in full screen mode.
     * @param playPos : double value for setting the play position in media player.
     * @param width  : integer value for setting media player full screen width size 
     * @param heigth: integer value for setting media player full screen height size
     */
    public PlayerFullScreen(String video, final double playPos, int width,int heigth) {		
		
    	super("Player Video Full Screen");  // call Window Superclass constructor 
	
    	/*
    	 * Add a Onclose event lister. When this event occurs fire a FullScreenEventHandler to
    	 * @SaStria media player event listeners.
    	 */
		addListener(new PanelListenerAdapter() {
			@Override
			public void onClose(Panel panel) { // OncLose, fire a FullScreenEventHandler to @SaStria media player event listeners.
				handleManager.fireEvent(new FullScreenEvent(medPlayer.getPlayPosition(),videoPlaying));
			}
		});
		
		videoPlaying=false;  // default play status is false(Video in pause) 
		
		
		/*
		 * Create and initialize the full screen media player 
		 */
		try {
			this.setSize(width-20, heigth-20);  // set media player full screen window size
			this.setPosition(10, 10);   // set the media player full screen window position
			medPlayer=new NativePlayer(video,true, "100%", "80%"); // create full screen media player			
			medPlayer.addPlayStateHandler(new PlayStateHandler() {   //PlayState Handler
				public void onPlayStateChanged(PlayStateEvent event) {	  // onPlayStateChanged update videoPlaying status			
				if(event.getPlayState()==PlayStateEvent.State.Started) // If video is in play
					 videoPlaying=true;
				else videoPlaying=false;
				}
			});
			
		} catch (LoadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PluginNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setLayout(new RowLayout());  //Sets window layout.
		
		VerticalPanel verticalPanel = new VerticalPanel();    // crate vertical panel  media player container
		verticalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);   // align middle in Vertical
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER); // align center in Horizontal
		add(verticalPanel);   //add vertical panel to this media player full screen window
		verticalPanel.setSize("100%", "100%"); //sets video player container size		
		verticalPanel.add(medPlayer); // add media player to its panel container  
		
		medPlayer.addLoadingProgressHandler(new LoadingProgressHandler() { //add addLoadingProgressHandler event listener
			public void onLoadingProgress(LoadingProgressEvent event) {  // onLoadingProgress change start timer to control when video was loaded. 
							
				Timer timerWaitForVideoLoaded=new Timer() {  // Timer to control when the video was loaded and ready to play.
					
					/*
					 * On time occurs check if video was loaded  
					 */
					@Override
					public void run() {
						if(medPlayer.getMediaDuration()>0){  // if media player duration >0 then the video was loaded 
							this.cancel();   // if video was loaded than stop this timer
							medPlayer.setPlayPosition(playPos); //if video was loaded then set start play position 
							medPlayer.setVisible(true);	// set media player as visible						
						}						
					}
				};				
				timerWaitForVideoLoaded.scheduleRepeating(100); // start timer scheduling to control when video was loaded 						
			}
		});
		medPlayer.setControllerVisible(true); // Set video controls as visible
		this.show();   // Open and show this full screen media player window 
		medPlayer.setVisible(false); // set media player invisible until video wasn't loaded
	}
	    

    
   /**
    * add a FullScreenCloseHandler handler
    * @param handler : the Full Screen video window close handler
    */
	public void addFullScreenCloseHandler(FullScreenEventHandler handler){
		handleManager.addHandler(FullScreenEvent.TYPE, handler);
	}
		
}
